﻿@* Generator: Template
    GeneratePrettyNames : true *@
@functions{
    public NFinal.Compile.SqlTemplate.Model.Page Model { get; set; }
}
#region	var @{@Model.varName};分页
			var __@{@Model.functionName}_@{@Model.varName}_command__ = new System.Data.SqlClient.SqlCommand("@{@Model.countSql}", @{@Model.connectionVarName});
			@if(Model.isTransaction){
			@:__@{@Model.functionName}_@{@Model.varName}_command__.Transaction=@{@Model.transactionVarName};
			}
			@if(Model.sqlVarParameters.Count>0){
			@:var __@{@Model.functionName}_@{@Model.varName}_parameters__=new System.Data.SqlClient.SqlParameter[@{@Model.sqlVarParameters.Count}];
			}
			@{int i=0;}
			@foreach(var sqlVarParameter in Model.sqlVarParameters){
			if(sqlVarParameter.field.length>0){
			@:__@{@Model.functionName}_@{@Model.varName}_parameters__[@i] = new System.Data.SqlClient.SqlParameter("@@@{@sqlVarParameter.name}",System.Data.SqlDbType.@{@sqlVarParameter.field.dbType},@{@sqlVarParameter.field.length});
			@:__@{@Model.functionName}_@{@Model.varName}_parameters__[@i].Value = @{@sqlVarParameter.csharpName};
			}else{
			@:__@{@Model.functionName}_@{@Model.varName}_parameters__[@i] = new System.Data.SqlClient.SqlParameter("@@@{@sqlVarParameter.name}",System.Data.SqlDbType.@{@sqlVarParameter.field.dbType});
			@:__@{@Model.functionName}_@{@Model.varName}_parameters__[@i].Value = @{@sqlVarParameter.csharpName};
			}
			i++;
			}
			@if(Model.sqlVarParameters.Count>0){
			@:__@{@Model.functionName}_@{@Model.varName}_command__.Parameters.AddRange(__@{@Model.functionName}_@{@Model.varName}_parameters__);
			}
			@{@Model.pageVarName}.recordCount =System.Convert.ToInt32(__@{@Model.functionName}_@{@Model.varName}_command__.ExecuteScalar());
			//令SqlParameter可以重复使用到不用的SqlCommand中
			__@{@Model.functionName}_@{@Model.varName}_command__.Parameters.Clear();
			__@{@Model.functionName}_@{@Model.varName}_command__.Dispose();
			@{@Model.pageVarName}.count = (@{@Model.pageVarName}.recordCount % @{@Model.pageVarName}.size==0)? @{@Model.pageVarName}.recordCount/@{@Model.pageVarName}.size:@{@Model.pageVarName}.recordCount/@{@Model.pageVarName}.size+1;
			//传页码时用的变量名
			if (@{@Model.pageVarName}.index > @{@Model.pageVarName}.count)
            {
                @{@Model.pageVarName}.index = @{@Model.pageVarName}.count;
            }
            if (@{@Model.pageVarName}.index < 1)
            {
                @{@Model.pageVarName}.index = 1;
            }
			@if(Model.hasGenericType){
			if(Model.isDeclaration){
			@:List<@{@Model.type}> @{@Model.varName}=new List<@{@Model.type}>();
			}
			}else{
			@:var @{@Model.varName} = new List<__@{@Model.functionName}_@{@Model.varName}__>();
            }
            //计算得到SQL语句>
			__@{@Model.functionName}_@{@Model.varName}_command__ = new System.Data.SqlClient.SqlCommand(string.Format("@{@Model.pageSql}",@{@Model.pageVarName}.size,(@{@Model.pageVarName}.index-1)*@{@Model.pageVarName}.size), @{@Model.connectionVarName});
			@if(Model.isTransaction){
			@:__@{@Model.functionName}_@{@Model.varName}_command__.Transaction=@{@Model.transactionVarName};
			}
			@if(Model.sqlVarParameters.Count>0){
			@:__@{@Model.functionName}_@{@Model.varName}_command__.Parameters.AddRange(__@{@Model.functionName}_@{@Model.varName}_parameters__);
			}
			var __@{@Model.functionName}_@{@Model.varName}_reader__= __@{@Model.functionName}_@{@Model.varName}_command__.ExecuteReader();
			if (__@{@Model.functionName}_@{@Model.varName}_reader__.HasRows)
			{
				while (__@{@Model.functionName}_@{@Model.varName}_reader__.Read())
				{
					@if(Model.hasGenericType){
					@:var __@{@Model.functionName}_@{@Model.varName}_temp__ = new @{@Model.type}();
					}else{
					@:var __@{@Model.functionName}_@{@Model.varName}_temp__ = new __@{@Model.functionName}_@{@Model.varName}__();
					}
					@{i=0;}
					@foreach(var field in Model.fields){
					if(field.allowNull){
					if(field.isValueType){
					@:if(!__@{@Model.functionName}_@{@Model.varName}_reader__.IsDBNull(@i)){__@{@Model.functionName}_@{@Model.varName}_temp__.@{@field.structFieldName} =@{@field.GetMethodConvert} __@{@Model.functionName}_@{@Model.varName}_reader__.@{@field.GetMethodName}(@i)@{@field.GetMethodValue};}
					}else{
					@:__@{@Model.functionName}_@{@Model.varName}_temp__.@{@field.structFieldName} =__@{@Model.functionName}_@{@Model.varName}_reader__.IsDBNull(@i)?null:@{@field.GetMethodConvert} __@{@Model.functionName}_@{@Model.varName}_reader__.@{@field.GetMethodName}(@i)@{@field.GetMethodValue};
					}
					}else{
					@:__@{@Model.functionName}_@{@Model.varName}_temp__.@{@field.structFieldName} =@{@field.GetMethodConvert} __@{@Model.functionName}_@{@Model.varName}_reader__.@{@field.GetMethodName}(@i)@{@field.GetMethodValue};
					}
					i++;
					}
					@{@Model.varName}.Add(__@{@Model.functionName}_@{@Model.varName}_temp__);
				}
			}
			__@{@Model.functionName}_@{@Model.varName}_reader__.Dispose();
			__@{@Model.functionName}_@{@Model.varName}_command__.Dispose();
			#endregion
			